package com.mikepenz.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.interactor.imp.RegistInteractorImp;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodType;
import com.mikepenz.utils.SystemUtils;
import com.mikepenz.view.RegiestView;

/**
 * Created by lilongfei on 15/11/8.
 */
public class RegistPresenterImp implements RequestListener {

    RegiestView view;
    Context context;
    RegistInteractorImp interactor;

    public RegistPresenterImp(Context context) {
        this.context = context;
        this.view = (RegiestView) context;
        interactor = new RegistInteractorImp(context, this);
    }

    @Override
    public void Success(String tag, Object result) {
        view.dismissLoad();
        if (tag.equals(MethodType.REGIST)) {
            view.showInfo("注册成功");
            ((Activity) context).finish();
        }
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showInfo(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showInfo(context.getString(R.string.system_error));
    }

    public void Regist() {
        String phone = view.getPhone();
        String pwd = view.getPwd();
        String sms = view.getSms();
        if (phone == null || phone.length() == 0) {
            view.setPhoneError("账号不能为空");
            return;
        }
        if (pwd == null || pwd.length() == 0) {
            view.setPwdError("密码不能为空");
            return;
        }

        if (pwd == null || pwd.length() > 18) {
            view.setPwdError("密码格式不符，6~18位之间");
            return;
        }
        if (sms == null || sms.length() == 0) {
            view.setSmsError("验证码不能为空");
            return;
        }

        if (!SystemUtils.isPhone(phone)) {
            view.setPhoneError("用户名必须是手机号");
            return;
        }

        view.showLoad();
        interactor.Regist(phone, pwd, sms);

    }


    public void getSms() {
        String phone = view.getPhone();
        if (!SystemUtils.isPhone(phone)) {
            view.setPhoneError("用户名必须是手机号");
            return;
        }
        interactor.getSms(phone);
        view.setButton("60秒", false);


        Thread thread = new Thread(new Runnable() {
            int count = 60;

            @Override
            public void run() {
                while (count > 0) {
                    count--;
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = count + "秒";
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                Message msg = new Message();
                msg.what = 1;
                msg.obj = "验证码";
                handler.sendMessage(msg);
            }
        });
        thread.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            String message = (String) msg.obj;
            if (what == 1) {
                view.setButton(message, true);
            } else {
                view.setButton(message, false);
            }
            super.handleMessage(msg);
        }
    };
}
